<?php

/**
 * @author zhuludong
 * @copyright 2018.11.14
 */
namespace Mininggame\Controller;
use Common\Controller\ApiController;
use Think\Model;
use Think\Log;

class LoginController extends ApiController {
    
    const GAME_TYPE = 'Mininggame';  // 游戏类型

    const DB_USER_NAME = 'mining_game.users'; // 游戏数据表

    const TOURIST = "mining:tourist:sid:%"; // 游戏游客用户

    const AUTH_USER = "mining:auth_user:sid:%";  // 授权用户

    const CACHE_KEY = 'mining';  // 游戏全局缓存键

    const MINING_COUNT_KEY = "mining:count"; // 渠道来源统计缓存键

    const MINING_RANK_LIST = "mining:rankList"; // 排行榜列表

    const MINING_DBID = 3; // 缓存数据编号

    // 登陆授权接口
    public function login()
    {
        $gameType = self::GAME_TYPE;
        $nickname = I('nickname','','string');
        $avatar = I('avatar','','string');
        $id = I('id','','int');
        $openid = I('openid','','string');
        $sid = I('source',0,'int');

        $data['openid'] = $openid;
        $data['nickname'] = $nickname;
        $data['avatar'] = $avatar;
        $data['id'] = $id;

        $dbname = self::DB_USER_NAME;
        $cacheKey = self::CACHE_KEY;
        $cacheval = sprintf(self::AUTH_USER,$sid);

        $dbId = self::MINING_DBID;
        
        $userInfo = $this->commLogin($dbname,$data,$cacheKey,$cacheval,$dbId);
        $userInfo['warehouse'] = json_decode($userInfo['warehouse'],true);
        $userInfo['serverTime'] = time();
        $incr = $userInfo['incr'];
        // redis
        //$this->getChannelCount(self::MINING_COUNT_KEY,$sid,'auth_user',$incr,$dbId);
        $this->response(0,'ok',$userInfo);
    }

    //未授权登陆接口
    public function loginNotAuth() {
        $code = I('code','','string');
        $sid = I('source',0,'int');
        $z_index = I('z_index',0,'int');
        if(!$code) {
            $this->response(1,'code不能为空');
        }
        $gameType = self::GAME_TYPE;
        $dbname = self::DB_USER_NAME;
        $cacheKey = self::CACHE_KEY;
        $cacheval = sprintf(self::TOURIST,$sid);
        $dbId = self::MINING_DBID;
        $userInfo = $this->commLoginNotAuth($gameType,$dbname,$code,$cacheKey,$cacheval,$dbId,$sid,$z_index);
        
        $userInfo['warehouse'] = json_decode($userInfo['warehouse'],true);
        $userInfo['serverTime'] = time();
        $incr = $userInfo['incr'];
        $this->getChannelCount(self::MINING_COUNT_KEY,$sid,'tourist',$incr,$dbId);
     
        $this->response(0,'ok',$userInfo);
    }

    public function text(){
        $dbname = self::DB_USER_NAME;
        $data['status2'] = 2;
        $data['source'] = 11000;
        $data['update_time'] = date('Y-m-d H:i:s');
        $ret = M($dbname)->where(array('id'=>'10018'))->save($data);

        echo M($dbname)->getLastSql();
    }


    // 清除掘金小美女 排行榜数据
    public function del_redis(){
        $redis = REDIS('activity');
        $redis->select(2);
        $hash_key = self::MINING_RANK_LIST;//   mining:rankList
        $cache_rank = $redis->hGetAll($hash_key);
        if($cache_rank){
            $del_reids = $redis->Del($hash_key);   // redis排行榜
            if($del_reids){
                echo "ok";
                log::write("提示：掘金排行榜redis清除成功！",'NOTIC');
            }else{
                echo "no";
                log::write("掘金排行榜redis清除失败！","ERROR");
            }
        }
    }
}